package com.android.View;

import java.io.File;
import java.util.Vector;

import com.android.GernerateKeySet.JavaFilter;
import com.android.GernerateKeySet.PreferenceInfo;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class CertSelectActivity extends Activity implements
		OnItemSelectedListener {
	
	TextView selection;
	TextView cur_CertfileName;
	Vector<String> mFileName;
	Spinner spin;
	Button btnOkay;
	PreferenceInfo preInfo;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.selectcert);
		selection = (TextView)findViewById(R.id.selection);
		spin = (Spinner) findViewById(R.id.spinner1);
		btnOkay = (Button) findViewById(R.id.btnok);
		cur_CertfileName = (TextView) findViewById(R.id.certname);
		mFileName = new Vector<String>();

		/** Preference **/

		SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
		preInfo = new PreferenceInfo(sharedPref);

		cur_CertfileName.setText(preInfo.getCertfile());
		
		mFileName.add(new String("인증서를 선택하세요"));
		spin.setOnItemSelectedListener(this);
		
		this.updateFileList();

	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, final int index,
			long arg3) {
		// TODO Auto-generated method stub
//		selection.setText(items[arg2]);
		Log.d("onItemSelected","onItemSelected");
		final String CertFileName;
		if(index > 0){

			CertFileName = mFileName.get(index);
			btnOkay.setEnabled(true);			
			selection.setText(mFileName.get(index)+" 를 선택하시겠습니까?");
			Log.d("test",CertFileName);
			Log.d("test",CertFileName.substring(0, CertFileName.length()-4));
			btnOkay.setOnClickListener(new OnClickListener() {				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					preInfo.setCertfile(CertFileName);
					Toast.makeText(CertSelectActivity.this, "인증서 선택 완료", Toast.LENGTH_LONG).show();
					onBackPressed();
				}
			});
		}
		else{
			btnOkay.setEnabled(false);
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		Log.d("onNothingSelected","onNothingSelected");
	}
	
	public void updateFileList(){

		
		String ext = Environment.getExternalStorageState();
		String path = null;
		
		if(ext.equals(Environment.MEDIA_MOUNTED)){
			path = Environment.getExternalStorageDirectory().getAbsolutePath() +"/cert/";
		}else{
			path = Environment.MEDIA_MOUNTED;			
		}
		File files = new File(path);
		
		if(files.listFiles(new JavaFilter())== null){
			
		}
		else if(files.listFiles(new JavaFilter()).length>0)
		{
			int count = 0;
			for(File file : files.listFiles(new JavaFilter())){
				mFileName.add(file.getName());
				count++;
			}
			
		}
		
		ArrayAdapter<String> filelist = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, mFileName);
		
		
		
		filelist.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(filelist);
	}

}